﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MiniJSON;
using System;

public class RandomLunchManager : MonoBehaviour {

    // 当前滚动餐厅列表集合
    public List<Restraunt> restrauntList;

    // 所有餐厅
	public Dictionary<int, List<Restraunt>> restrauntDic;
    
    // 滚动餐厅名称
    public Text text;

    //公司选择下拉列表
    public Dropdown companyDropDown;

    public bool stop = true;

    private int currentCompany = 0;

    // Use this for initialization
    void Start () {
		Init();
        
    }

	// 数据初始化
	void Init(){
        Dictionary<string, object> datas = GetDataFromFile.Instance.GetStreamingAssetsJsonData("Restraunt.json");

        restrauntDic = new Dictionary<int, List<Restraunt>>();

        List<object> temp = (List<object>)datas["list"];

        foreach(var restraunt in temp){
            Dictionary<string, object> restrauntData = restraunt as Dictionary<string, object>;

            Restraunt rest = new Restraunt();
            
            rest.Company = int.Parse(restrauntData["company"].ToString());
            rest.Type = (string)restrauntData["type"];
            rest.Name = (string)restrauntData["name"];
            rest.Distance = int.Parse(restrauntData["distance"].ToString());
            rest.AverageTime = int.Parse(restrauntData["averageTime"].ToString());
            rest.AverageCost = int.Parse(restrauntData["averageCost"].ToString());

            if(!restrauntDic.ContainsKey(rest.Company)){
                List<Restraunt> list = new List<Restraunt>();
                list.Add(rest);
                restrauntDic.Add(rest.Company, list);
            }else{
                restrauntDic[rest.Company].Add(rest);
            }
        }
	}
    
    // Update is called once per frame
    void Update () {
        
    }

    /// <summary>
    /// 滚动餐厅方法
    /// </summary>
    public IEnumerator ChooseRestraunt(){

        while(!stop){
            int rand = UnityEngine.Random.Range(0, restrauntList.Count);

            text.text = restrauntList[rand].Name;
            yield return new WaitForSeconds(0.1f);
        }

        stop = false;

    }

    /// <summary>
    /// 计算当前随机的餐厅集合
    /// </summary>
    public void ComputeRestraunt(){
        restrauntList = new List<Restraunt>();
        
        //TODO
        List<Restraunt> temp = restrauntDic[currentCompany];
        restrauntList.AddRange(temp);
    }

    public void OnCompanyChoose(){
        currentCompany = companyDropDown.value;
    }

}
